<?php
/**
 * 评论id修改
 * */

ini_set('display_errors','on');
error_reporting( E_ALL );

//表前缀
define('DB_TABLE_PRE','');
define('DB_DEBUG',true);

require_once '../conf/conf.php';
require_once 'class/Mysql.class.php';


$arid = isset($_POST['arid']) ? $_POST['arid'] : 0;

if( $arid ) c( $arid );

function c( $id ){
	
	$db = new Mysql();
	
	$ret = $db->getOne( 'topic_article', 'ar_id='.$id );
	if( empty( $ret ) ) exit('该文章不存在！');
	
	
	//取得比现有最大文章id大一的id
	$sql = "SELECT ar_id, web_type FROM topic_article ORDER BY ar_id DESC LIMIT 1";
	$row = $db->getOne( $sql );
	$new_id = $row['ar_id']+1;
	
	
	
	//把原文章ID改成新ID
	$db->update('topic_article', array('ar_id'=>$new_id), 'ar_id='.$id );
	
	
	
	//把相关文章中的原文章ID改成新ID，sql中不用replace，可能出现如11,1101,2111，原文章ID是11的情况
	$sql = "SELECT ar_id, related_article FROM topic_article WHERE FIND_IN_SET({$id},related_article)";
	$rows = $db->getAll( $sql );
	
	$str = '';
	foreach( $rows as $v ){
		$aa = explode(',', $v['related_article']);
		foreach( $aa as $kk=>$vv ){
			if( $vv == $id ) $aa[$kk] = $new_id;
		}
		
		$db->update('topic_article', array('related_article'=>implode(',', $aa)), 'ar_id='.$v['ar_id'] );
		
		$str .= $v['related_article'].'=>'.implode(',', $aa).' ';
	}
	
	
	//改文章模板中的文章ID,并生成
	switch( $row['web_type']){
		case 1://ws
			$ret = $db->update('Table_13', array('Field_19965'=>$new_id,'MendTime'=>time()), 'Field_19965='.$id );
			echo '<script src="http://cms.wondershare.com/bin/docList_13.php?action=pub&field_id=19965&key_id='.$new_id.'&ajax_pub=1&page=1"></script>';
			break;
			
		case 8://is
			$ret = $db->update('Table_71', array('Field_20681'=>$new_id,'MendTime'=>time()), 'Field_20681='.$id );
			echo '<script src="http://cms.wondershare.com/bin/docList_71.php?action=pub&field_id=20681&key_id='.$new_id.'&ajax_pub=1&page=1"></script>';
			break;
			
		case 2://am
			$ret = $db->update('Table_59', array('Field_20531'=>$new_id,'MendTime'=>time()), 'Field_20531='.$id );
			echo '<script src="http://cms.wondershare.com/bin/docList_59.php?action=pub&field_id=20531&key_id='.$new_id.'&ajax_pub=1&page=1"></script>';
			break;
	
	}
	
	echo '执行结果';
	echo '<br /><br />原文章ID：'.$id;
	echo '<br /><br />新文章ID：'.$new_id;
	echo '<br /><br />相关文章ID修改：'.$str;
	echo '<br /><br />文章模板ID修改：'.$ret ? 'OK' : 'NO';
	exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改文章ID</title>
<script type="text/javascript">
function C(){
	var v = document.getElementById('arid').value;
	if( !v ){
		alert('请输入文章ID');
		return false;	
	}
	return window.confirm('您确定要修改该文章ID？');
}
</script>
</head>

<body>
<form method="post" onsubmit="return C();" target="myframe">
请输入文章ID：<input type="text" size="6" name="arid" id="arid" />&nbsp;&nbsp;&nbsp;<input type="submit" value="确定" />
</form>

<iframe id="myframe" name="myframe" width="100%" height="200"></iframe>
注：<br />
1.当disqus后台出现test2.wondershare.com/...,或cms.wondershare.com/...，或check.wondershare.com/...时，用此功能。<br />
2.该功能将文章ID修改为“当前最大文章ID”+1，且会自动生成该文章页面，同步后外网会更新。<br />
3.该文章现有的评论会全部丢弃(请注意)。disqus后台评论需自己去删除或不删除(无影响)。<br />
4.当前支持ws,is,am三站自动生成，其它站需手动去生成，否则无法将修改后的文章同步出去。
</body>
</html>